﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fliter : MonoBehaviour
{

    public ComputeShader comShader;
	// Use this for initialization
	void Start ()
    {
        float[] w = new float[28 * 28];
        float[] images = new float[28*28];
	    images[10] = 1.0f;
	    w[10] = 1.0f;        
        float[] f = new float[3*3];
	    f[0] = 0.2f;
        f[1] = 0.2f;
        f[2] = 0.2f;
        f[3] = 0.2f;
        f[4] = 0.2f;
        f[5] = 0.2f;
        f[6] = 0.2f;
        f[7] = 0.2f;
        f[8] = 0.2f;
        float[] outvalue = new float[28 * 28];
        ComputeBuffer cbw = new ComputeBuffer(28*28,4);
        cbw.SetData(w);

        ComputeBuffer cbimage = new ComputeBuffer(28 * 28, 4);
        cbimage.SetData(images);

        ComputeBuffer cbf = new ComputeBuffer(3 * 3, 4);
        cbf.SetData(f);

        ComputeBuffer cboutvalue = new ComputeBuffer(28 * 28, 4);
        cboutvalue.SetData(outvalue);

        int kernel = comShader.FindKernel("CSMain");


        comShader.SetBuffer(kernel, "InW", cbw);
        comShader.SetBuffer(kernel, "InImage", cbimage);
        comShader.SetBuffer(kernel, "InFliter", cbf);
        comShader.SetBuffer(kernel, "Out", cboutvalue);
	    float a = Time.realtimeSinceStartup;
        for (int i = 0; i < 1000  ; i++)
	    {
            comShader.Dispatch(kernel, 28, 28, 1);
            cboutvalue.GetData(outvalue);
            for (int j = 0; j < outvalue.Length; j++)
            {
//                Debug.Log(outvalue[i]);
            }
        }
        float b = Time.realtimeSinceStartup;
        Debug.Log("b-a" + (b-a).ToString());
    }

    // Update is called once per frame
    void Update ()
    {
		
	}
}
